<template>
  <div class="app-container">
    <!-- 地块管理表-管理按钮 -->
    <div class="search-bar">
      <el-input v-model="searchData.number" placeholder="请输入地块编号" />
      <el-input v-model="searchData.dkmc" placeholder="请输入地块名称查询" />
      <el-select v-model="searchData.dklx" placeholder="请选择地块类型查询">
        <el-option value="" label="全部"></el-option>
        <el-option
          v-for="item in dict.dict.dkgl_type"
          :key="item.value"
          :value="item.value"
          :label="item.label"
        ></el-option>
      </el-select>
      <el-button
        v-waves
        type="primary"
        size="small"
        icon="el-icon-search"
        @click="searchBtnHandle"
        >查询</el-button
      >
      <el-button
        v-waves
        type="info"
        size="small"
        icon="el-icon-refresh"
        @click="resetTableList"
        >显示全部</el-button
      >
      <div style="float: right">
        <el-button
          v-waves
          type="primary"
          icon="el-icon-plus"
          @click="openAdd"
          size="small"
          v-permission="'jtnc-dkgl-krDkgl-add'"
          >添加地块
        </el-button>
      </div>
    </div>
    <!-- 地块管理表-列表 -->
    <el-table
      ref="dataTable"
      :data="tableData"
      stripe
      border
      @selection-change="handleTableSelectChange"
      v-loading="isLoading"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
        header-align="center"
      />
      <!-- <el-table-column label="地块编号" prop="number" align="center" /> -->
      <el-table-column label="地块名称" prop="dkmc" align="center" />
      <el-table-column label="地块面积（亩）" prop="dkmj" align="center" />
      <el-table-column label="地块位置" prop="dkwz" align="center" />
      <el-table-column label="传感器" prop="cgq" align="center" />
      <el-table-column label="地块类型" prop="dklx" align="center" />
      <el-table-column fixed="right" label="操作" width="120" align="center">
        <template v-slot="scope">
          <el-button
            type="text"
            style="color: #13ce66"
            size="small"
            @click="creatQrCode(scope.row)"
            >生成二维码</el-button
          >
          <el-button
            v-permission="'jtnc-dkgl-krDkgl-update'"
            type="text"
            size="small"
            @click="openUpdate(scope.row)"
            >修改
          </el-button>
          <el-button
            v-permission="'jtnc-dkgl-krDkgl-delete'"
            style="color: #ff6d6d"
            type="text"
            size="small"
            @click="deleteByIds(scope.row)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 地块管理表-分页 -->
    <el-pagination
      style="text-align: center; margin-top: 10px"
      layout="total,prev,pager,next,sizes,jumper"
      :page-size="pager.limit"
      :current-page="pager.page"
      :total="pager.totalCount"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <!-- 添加修改弹窗 -->
    <el-dialog
      :title="titleMap[dialogType]"
      :close-on-click-modal="dialogType !== 'view' ? false : true"
      :visible.sync="dialogFormVisible"
      @close="resetTemp"
      width="800px"
      :key="'myDialog' + dialogIndex"
    >
      <el-form
        ref="dataForm"
        :model="temp"
        label-position="right"
        label-width="100px"
        :disabled="dialogType === 'view'"
      >
        <!-- <el-form-item label="地块编号" prop="number" :rules="[]">
          <el-input v-model="temp.number" placeholder="请输入地块名称" />
        </el-form-item> -->
        <el-form-item
          label="地块名称"
          prop="dkmc"
          :rules="[
            { required: true, message: '请输入地块名称', trigger: 'blur' },
          ]"
        >
          <el-input v-model="temp.dkmc" placeholder="请输入地块名称" />
        </el-form-item>
        <el-form-item
          label="地块面积"
          prop="dkmj"
          :rules="[
            { required: true, message: '请输入地块面积', trigger: 'blur' },
          ]"
        >
          <el-input
            v-model="temp.dkmj"
            placeholder="请输入地块面积"
            type="number"
          >
            <template #append>亩</template>
          </el-input>
        </el-form-item>
        <el-form-item
          label="地块位置"
          prop="dkwz"
          :rules="[
            { required: true, message: '请输入地块位置', trigger: 'blur' },
          ]"
        >
          <el-input
            v-model="temp.dkwz"
            placeholder="请输入地块位置"
            type="textarea"
            max-length="30"
          />
        </el-form-item>
        <el-form-item label="传感器" prop="cgq" :rules="[]">
          <el-select v-model="temp.cgq" placeholder="请选择传感器">
            <el-option value=""></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="地块类型"
          prop="dklx"
          :rules="[
            { required: true, message: '请选择地块类型', trigger: 'blur' },
          ]"
        >
          <el-select v-model="temp.dklx" placeholder="请选择地块类型">
            <el-option
              v-for="item in dict.dict.dkgl_type"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-waves
          type="primary"
          v-if="dialogType !== 'view'"
          @click="saveData"
          >保存</el-button
        >
        <el-button v-waves @click="dialogFormVisible = false">取消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="二维码"
      :visible.sync="qrCodeVisible"
      width="300px"
      destroy-on-close
    >
      <div class="flexcenter">
        <div class="qrcode" ref="qrCodeUrl"></div>
      </div>
      <div class="flexcenter" style="margin-top: 20px">
        <el-button type="primary" @click="download">下载二维码</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import waves from "@/directive/waves";
import crudMixin from "@/mixin/crudMixin";
import QRCode from "qrcodejs2";
export default {
  directives: { waves },
  mixins: [crudMixin],
  dicts: ["dkgl_type"],
  data() {
    return {
      url: {
        list: "/jtnc/dkgl/krDkgl/list",
        add: "/jtnc/dkgl/krDkgl/add",
        info: "/jtnc/dkgl/krDkgl/getById",
        update: "/jtnc/dkgl/krDkgl/update",
        delete: "/jtnc/dkgl/krDkgl/delete",
        export: "/jtnc/dkgl/krDkgl/export/excel",
      },
      idName: "dkglId",
      qrCodeVisible: false,
      codeName: "",
    };
  },
  created() {
    this.loadTableList();
    this.resetTemp();
  },
  methods: {
    creatQrCode(row) {
      this.qrCodeVisible = true;
      this.codeName = row.dkmc;
      this.$nextTick(() => {
        new QRCode(this.$refs.qrCodeUrl, {
          text: row.dkglId, // 转换为二维码的内容
          width: 150,
          height: 150,
          colorDark: "#000000",
          colorLight: "#ffffff",
          correctLevel: QRCode.CorrectLevel.H,
        });
      });
    },
    download() {
      let canvasData = this.$refs.qrCodeUrl.getElementsByTagName("canvas");
      let a = document.createElement("a");
      let event = new MouseEvent("click"); // 创建一个单击事件
      a.href = canvasData[0].toDataURL("image/png");
      a.download = this.codeName;
      a.dispatchEvent(event);
    },
  },
};
</script>
<style lang="scss" scoped>
.search-bar {
  margin-bottom: 10px;
  ::v-deep {
    .el-input,
    .el-select {
      width: 180px;
      margin-right: 10px;
      margin-bottom: 5px;
    }
  }
}
.flexcenter {
  display: flex;
  justify-content: center;
}
</style>
